﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.Model;
using HZ.MBSM.DBUtility;
using System.Text;

public partial class Bidder_SetBidPwd1 : System.Web.UI.Page
{
    string prjid = "";
    Int64 Account_TBR;
    String pwdTrue, Rond;
    protected void Page_Load(object sender, EventArgs e)
    {
        System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumCompany);
        if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))
        {
            Account_TBR = SysFun.ToInt(((HZ.MBSM.Model.Account)Session["Account"]).AccountId);
            prjid = SysFun.IsSQL(Request.QueryString["projectid"]);
            prjid = System.Text.RegularExpressions.Regex.Replace(prjid, @"\D", "");
            String BSPriceSwitching = "0";
            String BSPricePassword = "";
            String BSRetinuepassword = "";

            if (!Page.IsPostBack)
            {
                Db db = new Db();
                System.Data.IDataReader dr;

                string sql = "select PriceSwitching,PricePassword,Retinuepassword from ProjectCompanyAttribution where AccountId='" + Account_TBR.ToString() + "' and ProjectId='" + prjid + "'";
                dr = db.DataReader(sql);
                while (dr.Read())
                {
                    BSPriceSwitching = SysFun.ToTrim(dr["PriceSwitching"]);
                    BSPricePassword = SysFun.ToTrim(dr["PricePassword"]);
                    BSRetinuepassword = SysFun.ToTrim(dr["Retinuepassword"]);

                }
                dr.Close();
                dr.Dispose();
                if (BSPriceSwitching == "0")
                {
                    Response.Write("<script>window.alert('报价开关没开');</script>");
                    sub.Visible = false;
                    ComeIn.Visible = false;
                    pwd.Visible = false;
                    confirmpwd.Visible = false;
                    Button1.Visible = false;
                }
                else if ((BSPricePassword == "" || BSRetinuepassword == "") && BSPriceSwitching != "0")
                {
                    label1.Text = "设置报价密码";
                    label2.Text = "再次输入密码";
                    ComeIn.Visible = false;
                    Button1.Visible = false;

                }
                else if (BSPricePassword != "" || BSRetinuepassword != "")
                {
                    label1.Text = "填入报价密码";
                    label2.Text = "填入随机密码";
                    sub.Visible = true;
                    sub.Text = "进入报价程序";
                    ComeIn.Visible = false;
                    Button1.Visible = false;
                }

                db.Close();
            }
        }
    }
    protected void sub_Click(object sender, EventArgs e)
    {
        if (Account_TBR != 0)
        {
            //判断是否是第一次设置密码

            if (label2.Text == "再次输入密码")//first login
            {
                //如果是第一次设置密码,那么将报价密码和随机密码保存到数据库中

                Random random = new Random();
                msg.Text = (random.Next(999999)).ToString();
                string _strRetinuePassword = msg.Text.ToString().Trim();//随机密码
                string _strPassword = pwd.Text.Trim();//报价密码
                string _strConfirmPassword = confirmpwd.Text.Trim();//确认报价密码
                if (_strPassword != "" && _strPassword == _strConfirmPassword)
                {
                    //将报价密码和随机密码保存到数据库中

                    string sql = "update ProjectCompanyAttribution set PricePassword='" + Md5check.MD5(_strPassword) + "',RetinuePassword='" + Md5check.MD5(_strRetinuePassword) + "' where  AccountId='" + Account_TBR + "' and ProjectId='" + prjid + "'";
                    Db db = new Db();
                    db.Command(sql);
                    HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
                    HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "更新", "ProjectCompanyAttribution", sql, "../Bidder/SetBidPwd1.aspx");

                    TextBox1.Visible = true;
                    pwd.Visible = false;
                    msg.Visible = false;
                    label2.Visible = false;
                    label1.Visible=false;
                    confirmpwd.Visible = false;
                    //将报价密码和随机密码放到页面中供投标人保存
                    Session["bidPassword"] = Md5check.MD5(_strPassword);
                    TextBox1.Text = "您输入的报价密码是“" + _strPassword + "”,您的随机密码是“" + _strRetinuePassword + "”,此2个密码在您再次进入报价界面和开标解密时必须输入,请务必牢记，若密码遗失将不可补救，此后果将由投标人自行承担。建议立即点击“保存报价、随机密码”按钮，按页面出现的操作提示保存密码文档（出现询问word转换器是否安装？提示，选“否”即可），生成的文件将存在电脑桌面上，文件名为“报价密码.doc”。请妥善保管此文件，以备再次进入报价程序及以后开标解密和竞价解密之用。";
                    ComeIn.Visible = true;
                    sub.Visible = false;
                    Button1.Visible = true;
                    TextBox3.Text = SysFun.PasswordEncrypt(pwd.Text);
                    TextBox4.Text = SysFun.PasswordEncrypt(msg.Text);
                    db.Close();
                }
                else
                {
                    Response.Write("<script>window.alert('密码不正确！！');</script>");
                }
            }

            else
            {
                //如果密码已经设置过了,那么判断输入得报价密码和随机密码是否正确
                Db db = new Db();
                string _strPassword = pwd.Text.Trim();//报价密码
                string _strConfirmPassword = confirmpwd.Text.Trim();//随机密码
                string sql = "select * From ProjectCompanyAttribution where AccountId='" + Account_TBR.ToString() + "' and ProjectId=" + prjid + " and PricePassword='" + Md5check.MD5(_strPassword) + "' and RetinuePassword ='" + Md5check.MD5(_strConfirmPassword) + "'";
                if (db.DataTable(sql).Rows.Count > 0)
                {
                    //如果断输入得报价密码和随机密码 正确,那么就自动跳转到报价页面
                    Session["Pass"] = "OK";
                    Session["bidPassword"] = Md5check.MD5(_strPassword);
                    Response.Redirect("BidPrice1.aspx?projectid=" + prjid);
                }
                else
                {
                    //如果断输入得报价密码和随机密码不正确,那么就提示用户密码错误

                    Response.Write("<script>window.alert('您输入得报价密码或随机密码错误！请重新输入！！！！');</script>");
                    msg.Text = "密码不正确";
                }
                db.Close();
            }
        }
        sub.Dispose();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Session["Pass"] = "OK";
        Response.Redirect("BidPrice1.aspx?projectid=" + prjid);

        ComeIn.Visible = false;
    }
    protected void Button1_Click1(object sender, EventArgs e)
    {
        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "GB2312";
        // 添加头信息，为"文件下载/另存为"对话框指定默认文件名 
        Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(TextBox2.Text.ToString()) + ".doc");
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
        Response.ContentType = "application/ms-word";
        this.EnableViewState = false;
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
        this.TextBox1.RenderControl(oHtmlTextWriter);

        string _strSave = oStringWriter.ToString().Substring(oStringWriter.ToString().IndexOf(">") + 1);
        _strSave = _strSave.Substring(0, _strSave.Length - 11);
        // 把文件流发送到客户端 
        Response.Write(_strSave);

        // 停止页面的执行


        Response.End();
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        //OverRide　为了使导出成Excel可行！

    }

}
